﻿-- =============================================
-- Author: tien dung
-- Create date: 26-07-2012
-- Description:	xu ly table Customer
-- =============================================
alter PROCEDURE ups_customer
	@flag int, -- 0: SEF, 1: INS, 2: DEL, 3: UPD, 4: SEL
	@id int =-1,
	@code nvarchar(100),
	@name nvarchar(100) ='',
	@telephone nvarchar(100) ='',
	@mobile nvarchar(100)='',
	@fax nvarchar(100)='',
	@address nvarchar(100)='',
	@email nvarchar(100)='',
	@description nvarchar(100)='',
	@debt_date_num int =-1,
	@tax_code nvarchar(25)='',
	@used int =-1,
	@error int output,
    @error_message nvarchar(200) output
AS
BEGIN
	set @error=0
	set @error_message='OK'

	if @flag = 0 -- select full
    begin
        select * From Customer
    end
    else if @flag = 1 -- insert
	begin
		begin try
			insert Customer([code],[name],[telephone],[mobile],[fax],[address],[email],[description],[debt_date_num],[tax_code],[used])
			values (@code,@name,@telephone,@mobile,@fax,@address,@email,@description,@debt_date_num,@tax_code,@used)
		end try
		
		begin catch
			SELECT @error=ERROR_NUMBER() , @error_message=isnull(ERROR_MESSAGE(),'')
		end catch	
    end
	else if @flag = 2 -- delete
	begin
		begin try
			delete Customer
			where [code]=@Code
		end try
		begin catch
			SELECT @error=ERROR_NUMBER() ,@error_message=isnull(ERROR_MESSAGE(),'')
		end catch	
    end
	else if @flag = 3 --update
	begin
		begin try
			update Customer
			set 
				[name] = @name,
				[telephone]=@telephone,
				[mobile]=@mobile,
				[fax]=@fax,
				[address]=@address,
				[email]=@email,
				[description]=@description,
				[debt_date_num] = @debt_date_num,
				[tax_code]=@tax_code,
				[used] = @used
			where [code]=@Code
		end try
		begin catch
			SELECT @error=ERROR_NUMBER() ,@error_message=isnull(ERROR_MESSAGE(),'')
		end catch	
    end
	else if @flag = 4 -- selecr with Where condition
	begin
		select * From Customer
		where [code]=@Code
    end

END


